import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";
import LoginView from "../views/LoginView.vue";
import RegisterView from "../views/RegisterView.vue";

Vue.use(VueRouter);

const routes = [
   // 商品路由
     {
      path: '/goods',
      name: 'Goods',
      component: () => import('../views/GoodsView.vue'),
      meta:{
        title:'商品列表'
      }
    },
  {
    // 网址导航路由
    path: "/webnavigat",
    name: "webnavigat",
    component: () => import("../views/WebNavigation.vue"),
  },
  {
    path: "/register",
    name: "RegisterView",
    // component: () => import('../views/RegisterView.vue'),
    component: RegisterView,
    meta: {
      title: "注册",
      requireRegister: true,
    },
  },
  {
    path: "/login",
    name: "LoginView",
    // component: () => import('../views/LoginView.vue'),
    component: LoginView,
    meta: {
      title: "登录",
      requireLogin: true,
    },
  },
  {
    path: "/",
    name: "home",
    component: HomeView,
    meta: {
      title: "首页",
    },
  },

  {
    path: "/aboutus",
    name: "AboutUs",
    component: () => import("../views/AboutUs.vue"),
    meta: {
      title: "关于我们",
    },
  },
  {
    path: "/aboutus/web",
    name: "WebNavigation",
    component: () => import("../views/WebNavigation.vue"),
    meta: {
      title: "网站地图",
    },
  },
  {
    path: "/aboutus/greement",
    name: "Aboutgreement",
    component: () => import("../views/Aboutgreement.vue"),
    meta: {
      title: "服务协议",
    },
  },
  //  关于里的 子路由 links 先写在外面
  {
    path: "/aboutus/links",
    name: "links",
    component: () => import("../views/AboutusLinks.vue"),
    meta: {
      title: "友情链接",
    },
  },
  // 关于里的 子路由 job 先写在外面
  {
    path: "/aboutus/job",
    name: "job",
    component: () => import("../views/AboutusJob.vue"),
    meta: {
      title: "加入我们",
    },
  },

  {
    path: "/help",
    name: "HelpCenter",
    component: () => import("../views/HelpCenter.vue"),
    meta: {
      title: "帮助中心",
    },
  },
  {
    path: "/logistics",
    name: "LogisticsView",
    component: () => import("../views/LogisticsView.vue"),
    meta: {
      title: "物流",
    },
  },
  {
    path: "/product",
    name: "ProductList",
    component: () => import("../views/ProductList.vue"),
    meta: {
      title: "手办商品",
    },
  },
  {
    path: "/details",
    name: "DetailsView",
    component: () => import("../views/DetailsView.vue"),
    meta: {
      title: "手办详情",
    },
  },
  {
    path: "/purchase",
    name: "代购单",
    component: () => import("../views/Purchase.vue"),
  },
  {
    path: "/cart",
    name: "ShoppingCart",
    component: () => import("../views/ShoppingCart.vue"),
    meta: {
      title: "购物车",
    },
  },
  {
    path: "/comments",
    name: "Comments",
    component: () => import("../views/CommentsView.vue"),
    meta: {
      title: "友好评论区",
    },
  },
  {
    path: "/personal",
    name: "PersonalCenter",
    component: () => import("../views/PersonalCenter.vue"),
    meta: {
      title: "个人中心",
    },
  },
];
// history
// 使用hash
const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  document.title = to.meta.title;
  // 判断浏览器临时存储的登录状态是否为ture
  if (sessionStorage.getItem("state")) {
    // 判断登录路由的requireLogin是否为turn
    if (to.meta.requireLogin) {
      next({
        path: "/",
      });
    } else {
      next();
    }
    // 判断注册路由requireRegister是否为turn
    if (to.meta.requireRegister) {
      next({
        path: "/",
      });
    } else {
      next();
    }

    next();
  } else {
    next();
  }
});
export default router;
